
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This file was initially generated by gen_to_start.go (add link), as a start
// of the Traffic Ops golang data model

package api

import (
	"encoding/json"
	_ "github.com/apache/incubator-trafficcontrol/traffic_ops/experimental/server/output_format" // needed for swagger
	"github.com/jmoiron/sqlx"
	null "gopkg.in/guregu/null.v3"
	"log"
	"time"
)

type Parameters struct {
	SpecificCatalog        null.String     `db:"specific_catalog" json:"specificCatalog"`
	Id                     int64           `db:"id" json:"id"`
	SpecificSchema         null.String     `db:"specific_schema" json:"specificSchema"`
	Name                   string          `db:"name" json:"name"`
	ConfigFile             string          `db:"config_file" json:"configFile"`
	SpecificName           null.String     `db:"specific_name" json:"specificName"`
	Value                  string          `db:"value" json:"value"`
	OrdinalPosition        null.Int        `db:"ordinal_position" json:"ordinalPosition"`
	CreatedAt              time.Time       `db:"created_at" json:"createdAt"`
	ParameterMode          null.String     `db:"parameter_mode" json:"parameterMode"`
	IsResult               null.String     `db:"is_result" json:"isResult"`
	AsLocator              null.String     `db:"as_locator" json:"asLocator"`
	ParameterName          null.String     `db:"parameter_name" json:"parameterName"`
	DataType               null.String     `db:"data_type" json:"dataType"`
	CharacterMaximumLength null.Int        `db:"character_maximum_length" json:"characterMaximumLength"`
	CharacterOctetLength   null.Int        `db:"character_octet_length" json:"characterOctetLength"`
	CharacterSetCatalog    null.String     `db:"character_set_catalog" json:"characterSetCatalog"`
	CharacterSetSchema     null.String     `db:"character_set_schema" json:"characterSetSchema"`
	CharacterSetName       null.String     `db:"character_set_name" json:"characterSetName"`
	CollationCatalog       null.String     `db:"collation_catalog" json:"collationCatalog"`
	CollationSchema        null.String     `db:"collation_schema" json:"collationSchema"`
	CollationName          null.String     `db:"collation_name" json:"collationName"`
	NumericPrecision       null.Int        `db:"numeric_precision" json:"numericPrecision"`
	NumericPrecisionRadix  null.Int        `db:"numeric_precision_radix" json:"numericPrecisionRadix"`
	NumericScale           null.Int        `db:"numeric_scale" json:"numericScale"`
	DatetimePrecision      null.Int        `db:"datetime_precision" json:"datetimePrecision"`
	IntervalType           null.String     `db:"interval_type" json:"intervalType"`
	IntervalPrecision      null.Int        `db:"interval_precision" json:"intervalPrecision"`
	UdtCatalog             null.String     `db:"udt_catalog" json:"udtCatalog"`
	UdtSchema              null.String     `db:"udt_schema" json:"udtSchema"`
	UdtName                null.String     `db:"udt_name" json:"udtName"`
	ScopeCatalog           null.String     `db:"scope_catalog" json:"scopeCatalog"`
	ScopeSchema            null.String     `db:"scope_schema" json:"scopeSchema"`
	ScopeName              null.String     `db:"scope_name" json:"scopeName"`
	MaximumCardinality     null.Int        `db:"maximum_cardinality" json:"maximumCardinality"`
	DtdIdentifier          null.String     `db:"dtd_identifier" json:"dtdIdentifier"`
	ParameterDefault       null.String     `db:"parameter_default" json:"parameterDefault"`
	Links                  ParametersLinks `json:"_links" db:-`
}

type ParametersLinks struct {
	Self string `db:"self" json:"_self"`
}

type ParametersLink struct {
	ID  int64  `db:"parameter" json:"id"`
	Ref string `db:"parameters_id_ref" json:"_ref"`
}

// @Title getParametersById
// @Description retrieves the parameters information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    Parameters
// @Resource /api/2.0
// @Router /api/2.0/parameters/{id} [get]
func getParameter(id int64, db *sqlx.DB) (interface{}, error) {
	ret := []Parameters{}
	arg := Parameters{}
	arg.Id = id
	queryStr := "select *, concat('" + API_PATH + "parameters/', id) as self"
	queryStr += " from parameters WHERE id=:id"
	nstmt, err := db.PrepareNamed(queryStr)
	err = nstmt.Select(&ret, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	nstmt.Close()
	return ret, nil
}

// @Title getParameterss
// @Description retrieves the parameters
// @Accept  application/json
// @Success 200 {array}    Parameters
// @Resource /api/2.0
// @Router /api/2.0/parameters [get]
func getParameters(db *sqlx.DB) (interface{}, error) {
	ret := []Parameters{}
	queryStr := "select *, concat('" + API_PATH + "parameters/', id) as self"
	queryStr += " from parameters"
	err := db.Select(&ret, queryStr)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return ret, nil
}

// @Title postParameters
// @Description enter a new parameters
// @Accept  application/json
// @Param                 Body body     Parameters   true "Parameters object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/parameters [post]
func postParameter(payload []byte, db *sqlx.DB) (interface{}, error) {
	var v Parameters
	err := json.Unmarshal(payload, &v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "INSERT INTO parameters("
	sqlString += "specific_catalog"
	sqlString += ",specific_schema"
	sqlString += ",name"
	sqlString += ",config_file"
	sqlString += ",specific_name"
	sqlString += ",value"
	sqlString += ",ordinal_position"
	sqlString += ",created_at"
	sqlString += ",parameter_mode"
	sqlString += ",is_result"
	sqlString += ",as_locator"
	sqlString += ",parameter_name"
	sqlString += ",data_type"
	sqlString += ",character_maximum_length"
	sqlString += ",character_octet_length"
	sqlString += ",character_set_catalog"
	sqlString += ",character_set_schema"
	sqlString += ",character_set_name"
	sqlString += ",collation_catalog"
	sqlString += ",collation_schema"
	sqlString += ",collation_name"
	sqlString += ",numeric_precision"
	sqlString += ",numeric_precision_radix"
	sqlString += ",numeric_scale"
	sqlString += ",datetime_precision"
	sqlString += ",interval_type"
	sqlString += ",interval_precision"
	sqlString += ",udt_catalog"
	sqlString += ",udt_schema"
	sqlString += ",udt_name"
	sqlString += ",scope_catalog"
	sqlString += ",scope_schema"
	sqlString += ",scope_name"
	sqlString += ",maximum_cardinality"
	sqlString += ",dtd_identifier"
	sqlString += ",parameter_default"
	sqlString += ") VALUES ("
	sqlString += ":specific_catalog"
	sqlString += ",:specific_schema"
	sqlString += ",:name"
	sqlString += ",:config_file"
	sqlString += ",:specific_name"
	sqlString += ",:value"
	sqlString += ",:ordinal_position"
	sqlString += ",:created_at"
	sqlString += ",:parameter_mode"
	sqlString += ",:is_result"
	sqlString += ",:as_locator"
	sqlString += ",:parameter_name"
	sqlString += ",:data_type"
	sqlString += ",:character_maximum_length"
	sqlString += ",:character_octet_length"
	sqlString += ",:character_set_catalog"
	sqlString += ",:character_set_schema"
	sqlString += ",:character_set_name"
	sqlString += ",:collation_catalog"
	sqlString += ",:collation_schema"
	sqlString += ",:collation_name"
	sqlString += ",:numeric_precision"
	sqlString += ",:numeric_precision_radix"
	sqlString += ",:numeric_scale"
	sqlString += ",:datetime_precision"
	sqlString += ",:interval_type"
	sqlString += ",:interval_precision"
	sqlString += ",:udt_catalog"
	sqlString += ",:udt_schema"
	sqlString += ",:udt_name"
	sqlString += ",:scope_catalog"
	sqlString += ",:scope_schema"
	sqlString += ",:scope_name"
	sqlString += ",:maximum_cardinality"
	sqlString += ",:dtd_identifier"
	sqlString += ",:parameter_default"
	sqlString += ")"
	result, err := db.NamedExec(sqlString, v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title putParameters
// @Description modify an existing parametersentry
// @Accept  application/json
// @Param   id              path    int     true        "The row id"
// @Param                 Body body     Parameters   true "Parameters object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/parameters/{id}  [put]
func putParameter(id int64, payload []byte, db *sqlx.DB) (interface{}, error) {
	var arg Parameters
	err := json.Unmarshal(payload, &arg)
	arg.Id = id
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "UPDATE parameters SET "
	sqlString += "specific_catalog = :specific_catalog"
	sqlString += ",specific_schema = :specific_schema"
	sqlString += ",name = :name"
	sqlString += ",config_file = :config_file"
	sqlString += ",specific_name = :specific_name"
	sqlString += ",value = :value"
	sqlString += ",ordinal_position = :ordinal_position"
	sqlString += ",created_at = :created_at"
	sqlString += ",parameter_mode = :parameter_mode"
	sqlString += ",is_result = :is_result"
	sqlString += ",as_locator = :as_locator"
	sqlString += ",parameter_name = :parameter_name"
	sqlString += ",data_type = :data_type"
	sqlString += ",character_maximum_length = :character_maximum_length"
	sqlString += ",character_octet_length = :character_octet_length"
	sqlString += ",character_set_catalog = :character_set_catalog"
	sqlString += ",character_set_schema = :character_set_schema"
	sqlString += ",character_set_name = :character_set_name"
	sqlString += ",collation_catalog = :collation_catalog"
	sqlString += ",collation_schema = :collation_schema"
	sqlString += ",collation_name = :collation_name"
	sqlString += ",numeric_precision = :numeric_precision"
	sqlString += ",numeric_precision_radix = :numeric_precision_radix"
	sqlString += ",numeric_scale = :numeric_scale"
	sqlString += ",datetime_precision = :datetime_precision"
	sqlString += ",interval_type = :interval_type"
	sqlString += ",interval_precision = :interval_precision"
	sqlString += ",udt_catalog = :udt_catalog"
	sqlString += ",udt_schema = :udt_schema"
	sqlString += ",udt_name = :udt_name"
	sqlString += ",scope_catalog = :scope_catalog"
	sqlString += ",scope_schema = :scope_schema"
	sqlString += ",scope_name = :scope_name"
	sqlString += ",maximum_cardinality = :maximum_cardinality"
	sqlString += ",dtd_identifier = :dtd_identifier"
	sqlString += ",parameter_default = :parameter_default"
	sqlString += " WHERE id=:id"
	result, err := db.NamedExec(sqlString, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title delParametersById
// @Description deletes parameters information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    Parameters
// @Resource /api/2.0
// @Router /api/2.0/parameters/{id} [delete]
func delParameter(id int64, db *sqlx.DB) (interface{}, error) {
	arg := Parameters{}
	arg.Id = id
	result, err := db.NamedExec("DELETE FROM parameters WHERE id=:id", arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}
